import { WS_ADDRESS } from '../config';

function useWebSocket (handleMessage) {
  const ws = new WebSocket(WS_ADDRESS);

  const init = () => {
    bindEvent()
  }

  function bindEvent() {
    ws.addEventListener('open', handleOpen, false);
    ws.addEventListener('close', handleClose, false);
    ws.addEventListener('error', handleError, false);
    ws.addEventListener('message', handleMessage, false);
  }

  function handleOpen(e) {console.log(e);}
  function handleClose(e) {console.log(e);}
  function handleError(e) {console.log(e);}
  // function handleMessage(e) {}

  init()

  return ws;
}

export default useWebSocket;